package com.scitc.pleasurevideo.datasql;

import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.pojo.VideoSourceVo;
import com.scitc.pleasurevideo.util.ConstUtil;
import com.scitc.pleasurevideo.x5webview.MyApplication;

import java.util.ArrayList;
import java.util.List;

public class VideoSourceSqlData extends DbSupport {

    private static VideoSourceSqlData dbDataManager;

    private VideoSourceSqlData() {

    }

    public static VideoSourceSqlData getDbDataManager() {
        if (dbDataManager == null) {
            dbDataManager = new VideoSourceSqlData();
            dbDataManager.checkWordBook();
        }
        return dbDataManager;
    }


    /**
     * 获取选择视频源的数量有几个
     * @return
     */
    public String getSelectVideoSourceNumber(){
        List<VideoSourceVo> sourceVos=selectVideoSourceList();
        if(sourceVos==null||sourceVos.size()<1){
            return "0/0";
        }
        int selectNumber=0;
        for (VideoSourceVo sourceVo : sourceVos) {
            if(sourceVo.getSource_url_is_select()){
                selectNumber++;
            }
        }
        return selectNumber+"/"+sourceVos.size();
    }


    /**
     * 初始化检查是否存在这个表
     */
    public void checkWordBook() {
        try {
            this.getSystemDbContext().queryList("select * from videosource", new String[]{});
        } catch (Exception e) {
            String videosource_SQL = "create table videosource(" +
                    "source_url_id bigint auto_increment," +
                    "source_url_title varchar(200)," +
                    "source_url_html_str varchar(200)," +
                    "source_url_search_str varchar(200)," +
                    "source_url_subtitle varchar(200)," +
                    "source_url_add_date varchar(200)," +
                    "source_url_use_count varchar(200)," +
                    "source_url_play_type varchar(200)," +
                    "source_url_is_download varchar(200)," +
                    "source_url_is_select varchar(200)," +
                    "source_url_is_use varchar(200), primary key(source_url_id));";
            executeSql(videosource_SQL);
        }
    }

    /**
     * 添加所有的 List<VideoSourceVo>
     * 首先要删除所有的 List<VideoSourceVo>
     * @param voList
     */
    public void saveAllVideoSource(List<VideoSourceVo> voList){
        deleteAllData();
        addAllVideoSource(voList);
    }

    /**
     * 添加所有的 List<VideoSourceVo>
     * 首先要删除所有的 List<VideoSourceVo>
     *     frist的意思是主要是每次打开程序都要从网络网址，所以设置为第一次
     *     但是主要的意思是，不替换改网址在本地数据的选中
     * @param voList
     */
    public void saveAllVideoSourceFrist(List<VideoSourceVo> voList){
        List<VideoSourceVo> oldVo=VideoSourceSqlData.getDbDataManager().selectVideoSourceList();
        for(int i=0;i<voList.size();i++){
            for (int j=0;j<oldVo.size();j++){
                if(voList.get(i).getSource_url_id().equals(oldVo.get(j).getSource_url_id())){
                    voList.get(i).setSource_url_is_select(oldVo.get(j).getSource_url_is_select());
                    oldVo.remove(j);
                    j--;
                }
            }
            if(oldVo==null||oldVo.size()<1){
                break;
            }
        }
        saveAllVideoSource(voList);
    }
    /**
     * 更新videoSourceVo 的数据
     * @param videoSourceVo
     */
    public void updataVideoSource(VideoSourceVo videoSourceVo){
        String updateSQL = "update videosource set " +
                "source_url_title =  ? ," +
                "source_url_html_str = ? ," +
                "source_url_search_str =  ? ," +
                "source_url_subtitle =  ? ," +
                "source_url_add_date =  ? ," +
                "source_url_use_count =  ? ," +
                "source_url_play_type =  ? ," +
                "source_url_is_download =  ? ," +
                "source_url_is_select =  ? ," +
                "source_url_is_use =  ? " +
                "where source_url_id = ? ";
        String[] datas = videoSourceVo.getStringArrayIdAfter();
        executeSql(updateSQL, datas);
    }

    /**
     * 添加 List<VideoSourceVo>
     * @param voList
     */
    private void addAllVideoSource(List<VideoSourceVo> voList){
        String insertSQL = "insert into videosource(" +
                "source_url_id," +
                "source_url_title," +
                "source_url_html_str," +
                "source_url_search_str," +
                "source_url_subtitle," +
                "source_url_add_date," +
                "source_url_use_count," +
                "source_url_play_type," +
                "source_url_is_download," +
                "source_url_is_select," +
                "source_url_is_use) values(?,?,?,?,?,?,?,?,?,?,?)";
        for(VideoSourceVo vo:voList){
            executeSql(insertSQL, vo);
        }
    }

    /**
     * 查询所有本地的网址
     * @return List<VideoSourceVo>
     */
    public List<VideoSourceVo> selectVideoSourceList() {
        String sql = "select * from videosource;";

        List<String[]> datas = this.getSystemDbContext().queryList(sql);
        List<VideoSourceVo> beans = new ArrayList<>();
        for (String[] data : datas) {
            VideoSourceVo videoSourceVo = getVideoSourceBean(data);
            beans.add(videoSourceVo);
        }
        return beans;
    }

    /**
     * 删除所有网址信息
     *
     */
    public void deleteAllData() {
        String deleteSQL = "delete from videosource;";
        executeSql(deleteSQL);
    }

    /**
     * 删除传入id的网址信息
     *
     * @param date
     * @param source_url_id
     */
    public void deleteSourceUrlId(String date, String source_url_id) {
        String deleteSQL = "delete from videosource  where source_url_id = ?;";
        String prs[] = new String[1];
        prs[0] = source_url_id;
        executeSql(deleteSQL, prs);
    }



    /**
     * 将查询到的数据转换成 videoSourceVo 对象
     *
     * @param data 查询到的数据
     * @return videoSourceVo 对象
     */
    private VideoSourceVo getVideoSourceBean(String[] data) {
        VideoSourceVo videoSourceVo=new VideoSourceVo(data);
        return videoSourceVo;
    }

    /**
     * 执行sql语句
     * @param sql           sql语句
     * @param videoSourceVo 将对象传入
     */
    private void executeSql(String sql, VideoSourceVo videoSourceVo) {
        String[] datas = videoSourceVo.getStringArray();
        executeSql(sql, datas);
    }

    /**
     * 执行sql
     * @param sql
     * @param datas
     */
    private void executeSql(String sql, String[] datas){
        getSystemDbContext().executeSQL(sql, datas);
    }

    /**
     * 执行sql
     * @param sql
     */
    private void executeSql(String sql){
        getSystemDbContext().execSQL(sql);
    }
}
